package org.spring.springboot.app.community.structureic.service.impl;

import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.spring.springboot.app.community.structure.dao.CommunityStructureDao;
import org.spring.springboot.app.community.structure.domain.po.CommunityStructurePO;
import org.spring.springboot.app.community.structureic.dao.HardIcDao;
import org.spring.springboot.app.community.structureic.domain.po.HardIcPO;
import org.spring.springboot.app.community.structureic.domain.vo.HardPositionVO;
import org.spring.springboot.app.community.structureic.service.IHardIcService;
import org.spring.springboot.util.IdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class HardIcServiceImpl implements IHardIcService {

    @Autowired
    HardIcDao hardIcDao;
    @Autowired
    private CommunityStructureDao communityStructureDao;

    @Override
    public List<HardIcPO> findPage(int pageNum, int pageSize, String infoId,String hardEquiId) {
        List<HardIcPO> list=hardIcDao.findPage(pageNum,pageSize,infoId,hardEquiId);
        return list;
    }

    @Override
    public List<HardPositionVO> findBinding(String hardEquiId,String info_id) {
        List<HardPositionVO> list=hardIcDao.findBinding(hardEquiId,info_id);
        for (HardPositionVO vo:list) {
            String[] ids=vo.getParentIds().split("\\,");
            String parentName="";
            for (int i=0;i<ids.length;i++){
                parentName+=communityStructureDao.selectByPrimaryKey(ids[i]).getName()+",";
            }
            vo.setParentNames(parentName);
        }
        return list;
    }

    @Override
    public List<HardPositionVO> findPageById(int pageNum, int pageSize, String structureId,String hardEquiId) {
        List<HardPositionVO> list=hardIcDao.findPageById(pageNum,pageSize,structureId,hardEquiId);
        return list;
    }

    @Override
    public int save(String id, MultipartFile file) {
        String[] str=file.getOriginalFilename().split("\\.");
        if(!str[str.length-1].equals("txt")){
            return 1;
        }
        //查询数据
        CommunityStructurePO po =communityStructureDao.selectByPrimaryKey(id);
        if(po==null){
            return 2;
        }
        //删除数据
        hardIcDao.deleteByStructureId(id);
        //导入数据
        try{
            InputStream inputStream = file.getInputStream();
            InputStreamReader is = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(is);
            StringBuffer sql=new StringBuffer("insert into community_structure_ic (structure_id,hardEquiId,block_index) values ");
            String ic="";
            int i=1;
            while ((ic = br.readLine()) != null) {
                if(ic!=null && ic.length()>0){
                    sql.append("('");
                    sql.append(id);
                    sql.append("','");
                    sql.append(ic);
                    sql.append("','");
                    if(i<=15000){
                        sql.append("1");
                    }else if(i>15000 && i<=30000){
                        sql.append("2");
                    }else if(i>30000 && i<=45000){
                        sql.append("3");
                    }else if(i>45000){
                        return 3;
                    }
                    sql.append("'");
                    sql.append("),");
                }
                i++;
            }
            String s=sql.toString().substring(0,sql.toString().length()-1);
            int re=hardIcDao.excuteSql(s);
            //查询小区中是否存在IC卡，不存在的话，添加，存在的话不管
            String ids=po.getParentIds();
            if(ids==null || ids.length()<1){
                //判断为小区---
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("office_id",po.getOfficeId());
                map.put("create_by",po.getCreateBy());
                map.put("update_by",po.getUpdateBy());
                map.put("structure_id",id);
                hardIcDao.copyDate(map);
            }else{
                //小区以下
                String info_id=po.getParentIds().split("\\,")[0];
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("office_id",po.getOfficeId());
                map.put("create_by",po.getCreateBy());
                map.put("update_by",po.getUpdateBy());
                map.put("structure_id",id);
                map.put("info_id",info_id);
                hardIcDao.copyDate1(map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteRelate(String id, String hardEquiId) {
        //查询出小区ID
        CommunityStructurePO po =communityStructureDao.selectByPrimaryKey(id);
        if(po!=null){
            return hardIcDao.deleteRelateIC(id,hardEquiId);
        }
        return 0;
    }
    @Override
    public int delete(String id, String hardEquiId) {
        //查询出小区ID
        CommunityStructurePO po =communityStructureDao.selectByPrimaryKey(id);
        if(po!=null){
            if(!po.getType().toString().equals("1")){
                return 0;
            }
            int i=hardIcDao.deleteHardIC(id,hardEquiId);
            int c=hardIcDao.deleteAllRelateIC(id,hardEquiId);
            return i+c;
        }
        return 0;
    }
}
